package model;

import java.sql.*;
import java.util.Vector;


public class MemberLevelDAO {

	
	//회원 등급 등록
	public boolean insert(MemberLevel ml) {
		Connection conn = ConnectionManager.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement("insert into memberlevel(lvno,lvname) values(?,?)");			
			ps.setInt(1, ml.getLvno());
			ps.setString(2, ml.getLvname());
			ps.executeUpdate();

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
	}

	//회원 등급 삭제
	public boolean delete(MemberLevel ml) {
		Connection conn = ConnectionManager.getConnection();
		try {
			Statement st = conn.createStatement();
			st.executeUpdate("delete from memberlevel where lvno=" + ml.getLvno());

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
	}

	//회원 등급 수정
	public boolean updateMemberLevel(MemberLevel ml) {
		Connection conn = ConnectionManager.getConnection();				
		try {			
			PreparedStatement ps = conn.prepareStatement("update memberlevel set lvname=? where lvno="+ml.getLvno());
			ps.setString(1, ml.getLvname());					
			ps.executeUpdate();
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
	}	
	
	//전체 회원 등급 수
	public int getTotalRows() {
		int totalRows = 0;
		Connection conn = ConnectionManager.getConnection();
		try {
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select count(*) from memberlevel");
			rs.next();
			totalRows = rs.getInt(1);
			rs.close();
			st.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
		return totalRows;
	}

	//회원 등급 상세 보기 
	public MemberLevel getMemberLevel(int lvno) {
		Connection conn = ConnectionManager.getConnection();
		MemberLevel level = null;
		try {
			Statement st = conn.createStatement();
			ResultSet rs = st
					.executeQuery("select * from memberlevel where lvno=" + lvno);
			if (rs.next()) {
				level = new MemberLevel();
				level.setLvno(rs.getInt(1));
				level.setLvname(rs.getString(2));
			}
			rs.close();
			st.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
		return level;
	}

	//회원 등급 목록
	public Vector getList_full() {
		Vector list = new Vector();
		Connection conn = ConnectionManager.getConnection();
		try {
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select * from memberlevel");
			while (rs.next()) {
				MemberLevel level = new MemberLevel();
				level.setLvno(rs.getInt(1));
				level.setLvname(rs.getString(2));
				
				list.add(level);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
		return list;
	}
	
	//회원 등급 목록 (최고관리자를 제외한다.)
	public Vector getList() {
		Vector list = new Vector();
		Connection conn = ConnectionManager.getConnection();
		try {
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select * from memberlevel where lvno != 9");
			while (rs.next()) {
				MemberLevel level = new MemberLevel();
				level.setLvno(rs.getInt(1));
				level.setLvname(rs.getString(2));
				
				list.add(level);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
		return list;
	}

}
